﻿using QLKetNoi;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLDVu.BO
{
    class ChiTietSuaChuaBO
    {
        QLDNDataContext Database = new QLDNDataContext();


        public IEnumerable LayThongTin(string MaSuaChua)
        {
            return from p in Database.CHITIETPHIEUSUACHUAs
                   join nd in Database.NGUOIDUNGs on p.MaNguoiDung equals nd.MaNguoiDung into lrj
                   from lr in lrj.DefaultIfEmpty()
                   join dv in Database.DICHVUs on p.MaDichVu equals dv.MaDichVu
                   where p.MaSuaChua == MaSuaChua.Trim()
                   select new { MaChiTietSuaChua = p.MaChiTietSuaChua, TenDichVu = dv.TenDichVu, TenNguoiDung = (p.MaNguoiDung == lr.MaNguoiDung) ? lr.TenNguoiDung : null };
        }


        public List<string[]> LayThongTinChiTietSuaChua(string MaSuaChua)
        {
            List<string[]> result = new List<string[]>();

            var Phieu = from p in Database.CHITIETPHIEUSUACHUAs
                        join nd in Database.NGUOIDUNGs on p.MaNguoiDung equals nd.MaNguoiDung into lrj
                        from lr in lrj.DefaultIfEmpty()
                        join dv in Database.DICHVUs on p.MaDichVu equals dv.MaDichVu
                        where p.MaSuaChua == MaSuaChua.Trim()
                        select new { MaChiTietSuaChua = p.MaChiTietSuaChua, TenDichVu = dv.TenDichVu, TenNguoiDung = (p.MaNguoiDung == lr.MaNguoiDung) ? lr.TenNguoiDung : null };

            foreach (var item in Phieu)
            {
                string[] s = new string[] { item.MaChiTietSuaChua, item.TenDichVu, item.TenNguoiDung };
                result.Add(s);
            }
            return result;
        }


        public List<CHITIETPHIEUSUACHUA> LayChiTietPhieuSuaTuMaPhieuSua(string MaPhieuSua)
        {
            return (from ctsc in Database.CHITIETPHIEUSUACHUAs
                    where ctsc.MaSuaChua == MaPhieuSua.Trim()
                    select ctsc).ToList();
        }


        public string LayMaDichVuTuTenDV(string TenDV)
        {
            return Database.DICHVUs.Where(p => p.TenDichVu == TenDV.Trim()).FirstOrDefault().MaDichVu;
        }

        public string LayMaNguoiDungTuTenND(string TenND)
        {
            return Database.NGUOIDUNGs.Where(p => p.TenNguoiDung == TenND.Trim()).FirstOrDefault().MaNguoiDung;
        }


        public void ThemChiTietPhieuSua(CHITIETPHIEUSUACHUA CTSuaChua)
        {
            Database.CHITIETPHIEUSUACHUAs.InsertOnSubmit(CTSuaChua);
            Database.SubmitChanges();
        }


        public void XoaChiTietPhieuSuaTuMaPhieuSua(string MaPhieuSua)
        {
            List<CHITIETPHIEUSUACHUA> list = new List<CHITIETPHIEUSUACHUA>();
            list = LayChiTietPhieuSuaTuMaPhieuSua(MaPhieuSua);
            foreach (var item in list)
            {
                XoaChiTietPhieuSua(item.MaChiTietSuaChua);
            }
        }


        private void XoaChiTietPhieuSua(string MaCTSuaChua)
        {
            var CTSuaChua = Database.CHITIETPHIEUSUACHUAs.Where(p => p.MaChiTietSuaChua == MaCTSuaChua).FirstOrDefault();
            Database.CHITIETPHIEUSUACHUAs.DeleteOnSubmit(CTSuaChua);
            Database.SubmitChanges();
        }


        public void SuaChiTietPhieuSua(string MaCTSuaChua, string TenNguoiDung)
        {
            var CTSuaChua = Database.CHITIETPHIEUSUACHUAs.Where(p => p.MaChiTietSuaChua == MaCTSuaChua.Trim()).FirstOrDefault();
            if (TenNguoiDung != null)
            {
                var NguoiDung = Database.NGUOIDUNGs.Where(n => n.TenNguoiDung == TenNguoiDung.Trim()).FirstOrDefault();
                CTSuaChua.MaNguoiDung = NguoiDung.MaNguoiDung;
            }
            Database.SubmitChanges();
        }


    }
}
